package system;
/*
 * Tham khảo http://docs.oracle.com/cd/E14571_01/apirefs.1111/e13951/core/index.html
 * */
import java.io.IOException;

import java.net.MalformedURLException;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class ManageGroup {

    private static MBeanServerConnection connection = null;
    private static ObjectName service = null;

    public ManageGroup() {
        super();
        try {
            connection = ManageWeblogic.initConnection();
            service =
                    ManageWeblogic.getAuthenticationProviderObjectName(ManageWeblogic.MBEAN_USERGROUP_INTERFACE);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public String listGroups() throws InstanceNotFoundException,
                                      MBeanException, ReflectionException,
                                      IOException {
        String listGroups =
            (String)connection.invoke(service, "listGroups", new Object[] { "*",
                                                                            100 },
                                      new String[] { "java.lang.String",
                                                     "java.lang.Integer" });
        return listGroups;
    }

    public void removeMemberFromGroup(String groupName,
                                      String member) throws InstanceNotFoundException,
                                                            MBeanException,
                                                            ReflectionException,
                                                            IOException {
        connection.invoke(service, "removeMemberFromGroup",
                          new Object[] { groupName, member },
                          new String[] { "java.lang.String",
                                         "java.lang.String" });
    }

    public void setGroupDescription(String groupName,
                                    String description) throws InstanceNotFoundException,
                                                               MBeanException,
                                                               ReflectionException,
                                                               IOException {
        connection.invoke(service, "setGroupDescription",
                          new Object[] { groupName, description },
                          new String[] { "java.lang.String",
                                         "java.lang.String" });
    }

    public boolean isMember(String parentGroupName,
                            String memberUserOrGroupName,
                            boolean recursive) throws InstanceNotFoundException,
                                                      MBeanException,
                                                      ReflectionException,
                                                      IOException {
        boolean isMember = (Boolean)connection.invoke(service, "isMember",
                          new Object[] { parentGroupName, memberUserOrGroupName,
                                         recursive },
                          new String[] { "java.lang.String",
                                         "java.lang.String",
                                         "java.lang.Boolean" });
        return isMember;
    }

    public boolean haveCurrent(String cursor) throws InstanceNotFoundException,
                                                  MBeanException,
                                                  ReflectionException,
                                                  IOException {
        boolean haveCurrent = (Boolean)connection.invoke(service, "haveCurrent", new Object[] { cursor },
                          new String[] { "java.lang.String" });
        return haveCurrent;
    }
    
    public String getCurrentName(String cursor) throws InstanceNotFoundException,
                                                          MBeanException,
                                                          ReflectionException,
                                                          IOException {
        String groups =
            (String)connection.invoke(service, "getCurrentName", new Object[] { cursor },
                                      new String[] { "java.lang.String" });
        return groups;
    }
    
    public void advance(String cursor) throws InstanceNotFoundException,
                                                MBeanException,
                                                ReflectionException,
                                                IOException {
        connection.invoke(service, "advance", new Object[] { cursor },
                          new String[] { "java.lang.String" });
    }
    
    public String getGroupDescription(String groupName) throws InstanceNotFoundException,
                                                               MBeanException,
                                                               ReflectionException,
                                                               IOException {
        
        String groupDescription =
            (String)connection.invoke(service, "getGroupDescription", new Object[] { groupName },
                                      new String[] { "java.lang.String" });
        return groupDescription;
    } 
    
    public void addMemberToGroup(String groupName, String memberUserOrGroupName) throws InstanceNotFoundException,
                                                                      MBeanException,
                                                                      ReflectionException,
                                                                      IOException {
        connection.invoke(service, "addMemberToGroup",
                          new Object[] { groupName, memberUserOrGroupName },
                          new String[] { "java.lang.String",
                                         "java.lang.String" });
    }


    //    Nhầm lẫn giữa Group và Role
    //    public void createRole(String resourceId, String roleName,
    //                           String expression) throws InstanceNotFoundException,
    //                                                     MBeanException,
    //                                                     ReflectionException,
    //                                                     IOException {
    //        connection.invoke(service, "createRole",
    //                          new Object[] { resourceId, roleName, expression },
    //                          new String[] { "java.lang.String",
    //                                         "java.lang.String",
    //                                         "java.lang.String" });
    //    }
    //
    //    public void removeRole(String resourceId,
    //                           String roleName) throws InstanceNotFoundException,
    //                                                   MBeanException,
    //                                                   ReflectionException,
    //                                                   IOException {
    //        connection.invoke(service, "removeRole",
    //                          new Object[] { resourceId, roleName },
    //                          new String[] { "java.lang.String",
    //                                         "java.lang.String" });
    //    }
    //
    //    public boolean roleExists(String resourceId,
    //                              String roleName) throws InstanceNotFoundException,
    //                                                      MBeanException,
    //                                                      ReflectionException,
    //                                                      IOException {
    //        boolean checkRoleExists = false;
    //        checkRoleExists =
    //                (Boolean)connection.invoke(service, "roleExists", new Object[] { resourceId,
    //                                                                                 roleName },
    //                                           new String[] { "java.lang.String",
    //                                                          "java.lang.String" });
    //        return checkRoleExists;
    //    }
    //
    //    public Object listRolesForResource(String resourceId) throws InstanceNotFoundException,
    //                                                                 MBeanException,
    //                                                                 ReflectionException,
    //                                                                 IOException {
    //        Object obj = new Object();
    //        obj =
    //connection.invoke(service, "listRolesForResource", new Object[] { resourceId },
    //                  new String[] { "java.lang.String" });
    //        return obj;
    //    }

}
